from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.charts import Bar, Timeline


def timeline_bar(data,range,rangetype,keyx,keyy,title,legend,color):
    tl = Timeline().add_schema(play_interval=3000, height=40, is_rewind_play=False, is_loop_play=True,
                               is_auto_play=True)
    for day in sorted(list(set(range))):
        x = data[data[rangetype] == day][keyx].values.tolist()  # 选取指定日期地区数据
        y1 = data[data[rangetype] == day][keyy].values.tolist()  # 选取指定日期业务数据

        bar = (
            Bar(init_opts=opts.InitOpts())
                .add_xaxis(x)
                .add_yaxis(legend, y1,color=color)

                .set_global_opts(
                title_opts=opts.TitleOpts(title=title,
                                          ),

                # xaxis_opts=opts.AxisOpts(type_="category", axislabel_opts={"rotate": 10})

            )
        )
        tl.add(bar, "{}".format(day))
    return tl
